package main

import (
	"context"
	"flag"
	"os"
	"fmt"
	
	jrpc "orchestrator/internal/infra/jsonrpc"
)

type modelOperation func(context.Context, *jrpc.Jclient, []byte) error

func main() {
	action := flag.String("action", "", "The action to perform (create, read, update, delete, count, list)")
	modelType := flag.String("model", "", "The type of model to use (t_batch, ...)")
	file := flag.String("f", "", "pararms  file")

	flag.Parse()

	// "unix:///tmp/my_rpc.sock"
	rpcSocketPath, ok := os.LookupEnv("RPC_URL")
	if !ok {
		rpcSocketPath = "unix:///tmp/my_rpc.sock"
		fmt.Println("RPC_SOCKET_PATH environment variable not set, default use ", rpcSocketPath)
	}

	c, err := jrpc.NewClient(rpcSocketPath)
	if err != nil {
		fmt.Printf("Error creating RPC client: %v\n", err)
		return
	}

	op, ok := ModelOperations[*modelType][*action]
	if !ok {
		fmt.Printf("Unsupported action: %s for model: %s\n", *action, *modelType)
		return
	}

	var b []byte

	if *file != "" {
		b, err = os.ReadFile(*file)
		if err != nil {
			fmt.Printf("Error reading file: %v\n", err)
			return
		}
	}

	err = op(context.Background(), c, b)
	if err != nil {
		fmt.Printf("Error performing %s operation: %v\n", *action, err)
		return
	}

	fmt.Printf("%s operation successful for model: %s\n", *action, *modelType)
}

var ModelOperations = map[string]map[string]modelOperation{
	"transaction": {
		"test": transactionTest,
	},
{{.mappers}}
}